package com.wx.activitys.mayday.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.activitys.base.utils.Common;
import com.wx.activitys.base.utils.EmojiFilter;
import com.wx.activitys.mayday.mapper.MaydayAreaMapper;
import com.wx.activitys.mayday.mapper.MaydayTeamMapper;
import com.wx.activitys.mayday.mapper.MaydayTeamMemberMapper;
import com.wx.activitys.mayday.model.MaydayArea;
import com.wx.activitys.mayday.model.MaydayTeam;
import com.wx.activitys.mayday.model.MaydayTeamMember;
import com.wx.activitys.mayday.model.MaydayTeamMemberKey;
import com.wx.activitys.mayday.service.MaydayService;
@Service("maydayService")
public class MaydayServiceImpl implements MaydayService{
	@Autowired
	MaydayAreaMapper areaMapper;
	@Autowired
	MaydayTeamMapper teamMapper;
	@Autowired
	MaydayTeamMemberMapper teamMemberMapper;
	
	public String updateArea(String areaId,String openId,String name) {
		int count = 0;
		try {
			//更新地区点击次数
			Integer size = areaMapper.selectByPrimaryKey(areaId).getComSize();
			MaydayArea area = new MaydayArea();
			area.setId(areaId);
			area.setComSize(size+1);			
			count = areaMapper.updateByPrimaryKeySelective(area);
			if (count == 1) {
				count = 0;
				//新增团队
				MaydayTeam team = new MaydayTeam();
				String teamId = Common.generateUUID();
				String nk= EmojiFilter.filterEmoji(name);
				
				team.setId(teamId);
				team.setAreaId(areaId);
				team.setCreateOpenId(openId);
				team.setCreateName(nk);
				team.setCreateTime(new Date());
				count = teamMapper.insert(team);
				if (count == 1) {
					return teamId;
				}
			}
		} catch (NullPointerException e) {
			e.printStackTrace();
			throw new RuntimeException();   
		}
			
		return "error";
	}

	public List<MaydayArea> selectPercentAreas() {
		return areaMapper.selectAllComSize();
	}

	public MaydayTeam selectTeamByTeamId(String teamId) {
		return teamMapper.selectByPrimaryKey(teamId);
	}

	public List<MaydayTeamMember> selectTeamMember(String teamId) {
		
		return teamMemberMapper.selectByTeamId(teamId);
	}

	public MaydayArea selectArea(String areaId) {
		
		return areaMapper.selectByPrimaryKey(areaId);
	}

	public String addTeamParter(String teamId, String openId, String img) {
		//查询团队创建者
		String createId = teamMapper.selectByPrimaryKey(teamId).getCreateOpenId();
		if (createId != null && createId.equals(openId)) {
			return "error";
		}
		//团队添加人员
		MaydayTeamMember member = new MaydayTeamMember();
		member.setTeamId(teamId);
		member.setOpenId(openId);
		member.setImg(img);
		member.setCreateTime(new Date());
		int add = teamMemberMapper.insertSelective(member);
		int count = 0;
		//查询地区id
		String areaId = teamMapper.selectByPrimaryKey(teamId).getAreaId();
		
		
		if ( areaId != null && add == 1 ) {
			//修改完成数
			Integer size = areaMapper.selectByPrimaryKey(areaId).getComSize();
			MaydayArea area = new MaydayArea();
			area.setId(areaId);
			area.setComSize(size+1);			
			count = areaMapper.updateByPrimaryKeySelective(area);
			if (count == 0) {
				throw new RuntimeException();
			}
		}
		
		return areaId;
	}

	public MaydayTeamMember selectTeamMemberByKey(String teamId, String openId) {
		MaydayTeamMemberKey key = new MaydayTeamMemberKey();
		key.setOpenId(openId);
		key.setTeamId(teamId);
		return teamMemberMapper.selectByPrimaryKey(key);
	}
	
}
